//
//  ViewController.m
//  TreeLimit
//
//  Created by vbn on 16/4/29.
//  Copyright © 2016年 vbn. All rights reserved.
//

#import "ViewController.h"
#import "Node.h"
#import "NodeTableView.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) NSMutableArray *lists;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.lists = @[].mutableCopy;
    
    Node *node1 = [Node new];
    node1.expand = YES;
    node1.childNode = @[].mutableCopy;
    node1.title = @"1";
    for (int i = 0; i < 10; i++) {
        Node *childNode = [Node new];
        childNode.title = [NSString stringWithFormat:@"1.%@",@(i).stringValue];
        [node1.childNode addObject:childNode];
    }
    
    [self.lists addObject:node1];
    
    node1 = [Node new];
    node1.childNode = @[].mutableCopy;
    node1.title = @"2";
    for (int i = 0; i < 10; i++) {
        Node *childNode = [Node new];
        childNode.title = [NSString stringWithFormat:@"2.%@",@(i).stringValue];
        [node1.childNode addObject:childNode];
    }
    
    [self.lists addObject:node1];
    
    node1 = [Node new];
    node1.expand = YES;
    node1.childNode = @[].mutableCopy;
    node1.title = @"3";
    for (int i = 0; i < 10; i++) {
        Node *childNode = [Node new];
        childNode.title = [NSString stringWithFormat:@"3.%@",@(i).stringValue];
        [node1.childNode addObject:childNode];
    }
    
    [self.lists addObject:node1];
    
    node1 = [Node new];
    node1.childNode = @[].mutableCopy;
    node1.title = @"4";
    for (int i = 0; i < 10; i++) {
        Node *childNode = [Node new];
        childNode.title = [NSString stringWithFormat:@"4.%@",@(i).stringValue];
        [node1.childNode addObject:childNode];
    }
    
    [self.lists addObject:node1];
    
    node1 = [Node new];
    node1.childNode = @[].mutableCopy;
    node1.title = @"5";
    for (int i = 0; i < 10; i++) {
        Node *childNode = [Node new];
        childNode.title = [NSString stringWithFormat:@"5.%@",@(i).stringValue];
        [node1.childNode addObject:childNode];
    }
    
    [self.lists addObject:node1];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.lists.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    Node *node = self.lists[section];
    if (node.expand) {
        return node.childNode.count + 1;
    } else {
        return 1;
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    Node *node = nil;
    if (indexPath.row == 0) {
        node = self.lists[indexPath.section];
    } else {
        node = self.lists[indexPath.section];
        node = node.childNode[indexPath.row-1];
    }
    
    cell.textLabel.text = node.title;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Node *node = nil;
    if (indexPath.row == 0) {
        node = self.lists[indexPath.section];
        node.expand = !node.expand;
    } else {
        node = self.lists[indexPath.section];
        node = node.childNode[indexPath.row-1];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [tableView reloadData];
    
}

@end
